﻿package com.cilico.components;

import com.cilico.ConfigManager;

/**
 * Controls which page to load for: collection list, POI list, and other generic lists that can be paged.
 * 
 * XXX: Page ID starts from 1.
 */
public final class Paging {
	/**
	 * The numbers of current and last pages returned by the server, if POIs in the channel is returned in multiple
	 * pages.
	 */
	public int currentPage_;
	public int lastPage_;
	public int downloadPage_;// for download use.

	/** Creates a new instance of Paging */
	public Paging() {
		currentPage_ = 1;
		lastPage_ = 1;
		downloadPage_ = currentPage_;
	}

	void assign(Paging p) {
		currentPage_ = p.currentPage_;
		lastPage_ = p.lastPage_;
		downloadPage_ = currentPage_;
	}

	/**
	 * Pre-defined POIs per page. This is also hard-coded into server.
	 */
	public int pageSize() {
		return ConfigManager.POIS_PER_SCREEN;
	}

	public void setLastPage(int p) {
		lastPage_ = p;
	}

	public void setCurrentPage(int i) {
		currentPage_ = i;
		downloadPage_ = currentPage_;
	}

	public void nextPage() {
		downloadPage_ = currentPage_;
		if (currentPage_ >= lastPage_)
			downloadPage_ = currentPage_ = lastPage_;
		else
			++downloadPage_;
	}

	public void prevPage() {
		downloadPage_ = currentPage_;
		if (currentPage_ <= 1)
			downloadPage_ = currentPage_ = 1;
		else
			--downloadPage_;
	}

	public boolean atStart() {
		return currentPage_ == 1;
	}

	public boolean atEnd() {
		return currentPage_ >= lastPage_;
	}

	void reset() {
		currentPage_ = lastPage_ = 1;
		downloadPage_ = currentPage_;
	}

}
